﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using EnterpriseFund.Common;
using DevExpress.Web.Mvc;
using System.Data;

namespace EnterpriseFund.Controllers
{
        [OutputCache(Duration = 0, VaryByParam = "none")]
    public class UpLevelAllowanceAuditController : AdminBaseController
    {
        //
        // GET: /UpLevelAllowanceAudit/
            [AuthorityActionFilter("m_UpLevelAllowanceAudit", Order = 1)]
            [LogPageActionFilter("其他补助审核", Order = 2)]
        public ActionResult Index()
        {
            string strwhere = " FStatus='N' and FIsAudit='Y' ";
            List<Model.UpLevelAllowance> UpLevelAllowanceList = new List<Model.UpLevelAllowance>();
            UpLevelAllowanceList = GetUpLevelAllowanceListList(strwhere, 1);
            return PartialView(UpLevelAllowanceList);
        }

        public ActionResult UpLevelAllowanceAuditEdit(string FUpLevelAllowanceID)
        {
            BLL.UpLevelAllowance upLevelAllowanceBLL = new BLL.UpLevelAllowance();
            BLL.UpLevelAllowanceEntry upLevelAllowanceEntryBLL = new BLL.UpLevelAllowanceEntry();
            Model.UpLevelAllowance model = new Model.UpLevelAllowance();
            BLL.UpLevelAllowanceCls upLevelAllowanceClsBLL = new BLL.UpLevelAllowanceCls();
            ViewData["UpLevelAllowanceClsList"] = upLevelAllowanceClsBLL.GetModelList("");
            if (FUpLevelAllowanceID != null && FUpLevelAllowanceID != "")
            {
                model = upLevelAllowanceBLL.GetModel(FUpLevelAllowanceID);
            }
            DataTable dt = upLevelAllowanceEntryBLL.GetList(" FUpLevelAllowanceID = '" + FUpLevelAllowanceID + "'").Tables[0];
            ViewData["showdata"] = GetShowData(dt);
            ViewData["FUpLevelAllowanceID"] = FUpLevelAllowanceID;
            BLL.FinanceProp FinancePropBLL = new BLL.FinanceProp();
            ViewData["FinancePropList"] = FinancePropBLL.GetModelList("");
            return View("UpLevelAllowanceAuditEdit", model);
        }

        public DataTable GetShowData(DataTable dt)
        {
            dt.Columns["FUpLevelAmount"].DataType = typeof(decimal);
            decimal sumse = dt.AsEnumerable().Sum(p => p.Field<decimal>("FUpLevelAmount")) + dt.AsEnumerable().Sum(p => p.Field<decimal>("FLocalAmount"));
            DataRow dr = dt.NewRow();
            dr["NSRMC"] = "合计";
            dr["FUpLevelAmount"] = sumse;
            dt.Rows.Add(dr);
            return dt;

        }

        public ActionResult UpLevelAllowanceAuditListPartial()
        {
            int pageIndex = Request.Params["pageIndex"] == null ? 0 : int.Parse(Request.Params["pageIndex"].ToString());
            int pageCount = Request.Params["PageCount"] == null ? 0 : int.Parse(Request.Params["PageCount"].ToString());
            int pageRowSize = Request.Params["pageRowSize"] == null ? 0 : int.Parse(Request.Params["pageRowSize"].ToString());
            int pageRowCount = Request.Params["pageRowCount"] == null ? 0 : int.Parse(Request.Params["pageRowCount"].ToString());
            string callbackArg = DevExpressHelper.CallbackArgument;

            string command = Request.Params["command"] == null ? "" : Request.Params["command"].ToString();

            if (command == "PAGERONCLICK")
            {
                string[] args = callbackArg.Split(new char[1] { '|' });
                string arg = args[args.Count() - 1];

                switch (arg)
                {
                    case "PBN;":
                        {
                            //下一页
                            pageIndex = pageIndex + 2;
                            break;
                        }
                    case "PBP;":
                        {
                            //上一页
                            //pageIndex = pageIndex;
                            break;
                        }
                    case "PBL;":
                        {
                            //最后一页
                            pageIndex = pageCount;
                            break;
                        }
                    case "PBF;":
                        {
                            //第一页
                            pageIndex = 1;
                            break;
                        }
                    default:
                        {
                            //点击数字页码
                            string tempIndex = arg.Replace("PN", "");
                            tempIndex = tempIndex.Replace(";", "");
                            if (RegExp.IsNumeric(tempIndex))
                            {
                                pageIndex = int.Parse(tempIndex) + 1;
                            }
                            break;
                        }

                }
            }
            else
            {
                pageIndex = pageIndex + 1;
            }
            string strwhere = " FStatus='N' and FIsAudit='Y' ";

            List<Model.UpLevelAllowance> UpLevelAllowanceList = new List<Model.UpLevelAllowance>();
            UpLevelAllowanceList = GetUpLevelAllowanceListList(strwhere, pageIndex);
            return PartialView("UpLevelAllowanceAuditListPartial", UpLevelAllowanceList);
        }

        /// <summary>
        /// 获取定义的工作时间数据
        /// </summary>
        /// <param name="strWhere">查询条件</param>
        /// <param name="pageIndex">第几页</param>
        /// <returns>查询出来的数据</returns>
        private List<Model.UpLevelAllowance> GetUpLevelAllowanceListList(string strWhere, int pageIndex)
        {
            BLL.Common commonBll = new BLL.Common();
            BLL.UpLevelAllowance UpLevelAllowanceBLL = new BLL.UpLevelAllowance();
            string fiedls = "FUpLevelAllowanceID";
            int total;
            DataSet ds = commonBll.GetListByPage("fund.v_UpLevelAllowance", fiedls, Common.GlobalConstant.CONSTRING_PAGESIZE, pageIndex, strWhere, "FUpLevelAllowanceID", out total);
            List<Model.UpLevelAllowance> UpLevelAllowanceList = UpLevelAllowanceBLL.DataTableToList(ds.Tables[0]);
            List<Model.UpLevelAllowance> UpLevelAllowanceAllList = new List<Model.UpLevelAllowance>();
            total = UpLevelAllowanceBLL.GetRecordCount2(strWhere);
            int pageRowSize = Common.GlobalConstant.CONSTRING_PAGESIZE;
            for (int i = 0; i < total; i++)
            {
                UpLevelAllowanceAllList.Add(null);
            }
            for (int i = 0; i < pageRowSize; i++)
            {
                if (UpLevelAllowanceList.Count > i)
                {
                    UpLevelAllowanceAllList[((pageIndex - 1) * pageRowSize) + i] = UpLevelAllowanceList[i];
                }
            }

            return UpLevelAllowanceAllList;
        }

        public ActionResult Save()
        {
            string FUpLevelAllowanceID = Fetch.Post("hidFUpLevelAllowanceID");
            try
            {
                string FStatus = Fetch.Post("hidFStatus");
                Model.UpLevelAllowance upLevelAllowanceModel = new Model.UpLevelAllowance();
                upLevelAllowanceModel.FUpLevelAllowanceID = FUpLevelAllowanceID;
                switch (FStatus)
                {
                    case "open":
                        upLevelAllowanceModel.FStatus = "A";
                        upLevelAllowanceModel.FIsReturn = "N";
                        break;
                    case "Return":
                        upLevelAllowanceModel.FStatus = "D";
                        upLevelAllowanceModel.FIsReturn = "Y";
                        break;
                }

                BLL.UpLevelAllowance upLevelAllowanceBLL = new BLL.UpLevelAllowance();
                upLevelAllowanceBLL.UpdateStatus(upLevelAllowanceModel);
                ViewData["jsAlert"] = "操作成功";
            }
            catch(Exception ex)
            {
                ViewData["jsAlert"] = ex.Message;
            }

            return UpLevelAllowanceAuditEdit(FUpLevelAllowanceID);
        }
    }
}
